import time
import datetime
import re
import random


def get_now():
    return time.strftime("%Y-%m-%d %H-%M-%S", time.localtime())


def format_date(s):
    now = datetime.datetime.now()
    if s == '刚刚':
        return time.strftime("%Y-%m-%d", time.localtime())
    if re.match(r'[0-9]+分钟前', s) is not None:
        return (now - datetime.timedelta(minutes=int(s[:s.index('分钟前')]))).strftime("%Y-%m-%d")
    if re.match(r'[0-9]+小时前', s) is not None:
        return (now - datetime.timedelta(hours=int(s[:s.index('小时前')]))).strftime("%Y-%m-%d")
    if re.match(r'昨天 [0-9]{2}:[0-9]{2}', s) is not None:
        return (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d ")
    try:
        return time.strftime("%Y-%m-%d", time.strptime(s, '%Y-%m-%d'))
    except ValueError as e:
        # print(str(e))
        try:
            return time.strftime('%Y-%m-%d', time.strptime(s, "%a %b %d %H:%M:%S %z %Y"))
        except ValueError as e:
            # print(str(e))
            try:
                s = str(datetime.datetime.now().year) + '-' + s
                return time.strftime("%Y-%m-%d", time.strptime(s, '%Y-%m-%d'))
            except ValueError as e:
                # print(str(e))
                return None


def format_datetime(s):
    now = datetime.datetime.now()
    if s == '刚刚':
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if re.match(r'[0-9]+分钟前', s) is not None:
        return (now - datetime.timedelta(minutes=int(s[:s.index('分钟前')]))).strftime("%Y-%m-%d %H:%M:%S")
    if re.match(r'[0-9]+小时前', s) is not None:
        return (now - datetime.timedelta(hours=int(s[:s.index('小时前')]))).strftime("%Y-%m-%d %H:%M:%S")
    if re.match(r'昨天 [0-9]{2}:[0-9]{2}', s) is not None:
        return (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d ") + s.split(' ')[1] + ':00'
    if re.match(r'[0-9]{2}-[0-9]{2}', s) is not None:
        return time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(str(now.year) + '-' + s, '%Y-%m-%d'))
    return time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(s, "%Y-%m-%d"))


def sleep_random(inf):
    s = random.randint(4, 15)
    print(inf, ', 随机等待4-15秒, 本次等待 {} 秒:'.format(s), end='')
    for i in range(s, 0, -1):
        print('{} '.format(i), end='')
        time.sleep(1)
    print()


print(format_datetime('1分钟前'))